﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml;

namespace Package_Tool
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnInput_Click(object sender, EventArgs e)
        {
            if (folderInput.ShowDialog() == DialogResult.OK)
            {
                txtInput.Text = folderInput.SelectedPath;
            }
        }

        private void btnOutput_Click(object sender, EventArgs e)
        {
            if (folderOutput.ShowDialog() == DialogResult.OK)
            {
                txtOutput.Text = folderOutput.SelectedPath;
            }
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            if (!Directory.Exists(txtInput.Text))
            {
                txtResult.Text += "输入路径不存在！" + System.Environment.NewLine + System.Environment.NewLine;
                return;
            }
            if (!Directory.Exists(txtOutput.Text))
            {
                txtResult.Text += "输出路径不存在！" + System.Environment.NewLine + System.Environment.NewLine;
                return;
            }

            DirectoryInfo inputdir = new DirectoryInfo(txtInput.Text);
            int mdlcount = 0;
            FileInfo[] files = inputdir.GetFiles();
            //创建mdl泛型
            Dictionary<int, FileInfo> mdldir = new Dictionary<int, FileInfo>();
            //检索指定文件夹下的所有mdl文件
            for (int i = 0; i < files.Length; i++)
            {
                string extension = files[i].Extension;
                if (extension == ".mdl")
                    mdldir.Add(mdlcount++, files[i]);
            }

            int count = 0;
            for (int i = 0; i < mdlcount; i++)
            {

                FileInfo mdlfile;
                string mdlname = "";
                //得到指定键的泛型的值
                mdldir.TryGetValue(i, out mdlfile);
                mdlname = Path.GetFileNameWithoutExtension(mdlfile.Name);
                txtResult.Text += "-------------------" + mdlfile.Name + "-------------------" + System.Environment.NewLine + System.Environment.NewLine;
                string outputdir = txtOutput.Text + @"\" + (++count) + "_" + mdlname;
                //创建对应mdl的文件夹
                System.IO.Directory.CreateDirectory(outputdir);
                txtResult.Text += "创建" + outputdir + "文件夹" + System.Environment.NewLine + System.Environment.NewLine;
                //将cs、mdl、xml文件拷贝到文件夹下
                File.Copy(AppDomain.CurrentDomain.BaseDirectory + "\\template\\Program.cs", outputdir + "\\Program.cs", true);
                File.Copy(mdlfile.FullName, outputdir + "\\" + mdlfile.Name, true);
                //替换掉原来的占位符
                string programCode = File.ReadAllText(outputdir + "\\Program.cs").Replace("$(mdl)", mdlname + ".mdl");
                File.WriteAllText(outputdir + "\\Program.cs", programCode);
                txtResult.Text += "成功！" + System.Environment.NewLine + System.Environment.NewLine;

            }

        }

        private void btnOpen_Click(object sender, EventArgs e)
        {
            if (Directory.Exists(txtOutput.Text))
                System.Diagnostics.Process.Start(txtOutput.Text);
        }

    }
}
